<?php


namespace app\api\controller\v34;


use app\api\controller\v2\Auth;
use app\common\cachemodel\zl1_relation\Zl1RelationAnalysisIdea;
use app\common\cachemodel\zl1_relation\Zl1RelationAnalysisReport;
use app\common\lib\astrolog\AuthenticatePlayboy;
use app\common\lib\astrolog\HowToWooHim;
use think\Env;

class UserRelationAnalysis extends Auth
{
    /**
     * 获取 怎么追求Ta 解锁记录列表
     */
    public function getFollowList()
    {
        $pageData = (new Zl1RelationAnalysisReport())->getListPage($this->user['id'], 'id', ['rid1', 'rid2', 'create_time', 'id', 'recode_info', 'content', 'status'], 1, 'desc', 0);

        $resData = [];
        foreach ($pageData['data'] as $itemData)
        {
            $itemData = arrayKeyData2int($itemData, ['id', 'uid', 'status', 'gid', 'rid1', 'rid2', 'create_time']);

            if (isset($itemData['content']) && $itemData['content'])
            {
                $itemData['content'] = json_decode($itemData['content'], true);
            }

            if (isset($itemData['recode_info']) && $itemData['recode_info'])
            {
                $itemData['recode_info'] = json_decode($itemData['recode_info'], true);
            }
            // 计算状态 1待计算 2计算成功 3计算失败 4已退款 => 转换 状态 1=>计算中 2=>已解锁 3=>已退款
            $status = [
                1 => 1,
                2 => 2,
                3 => 1,
                4 => 3,
            ];

            if (in_array($itemData['status'], [1, 3], true))
            {   // 计算中
                $content = HowToWooHim::getInstance()->setUserRecord(array_values($itemData['recode_info']))->free();
                $star = $content['difficulty'] ?? 3; // 默认三星
            } else
            {   // 计算成功 ｜ 已退款
                $star = $itemData['content']['difficulty'] ?? 3; // 默认三星
            }

            $resData[] = [
                'id' => $itemData['id'],
                'title' => '追求难度星级',
                'status' => $status[$itemData['status']],
                'star' => $star,
                'recode1' => [
                    'avatar' => $itemData['recode_info']['recode1']['avatar'],
                    'name' => $itemData['recode_info']['recode1']['id'] == $this->user['rid'] ? '自己' : $itemData['recode_info']['recode1']['name'],
                ],
                'recode2' => [
                    'avatar' => $itemData['recode_info']['recode2']['avatar'],
                    'name' => $itemData['recode_info']['recode2']['id'] == $this->user['rid'] ? '自己' : $itemData['recode_info']['recode2']['name'],
                ],
                'target' => $status[$itemData['status']] === 2 ? 'link' : 'local',
                'link' => $status[$itemData['status']] === 2 ? Env::get('host.domain') . "/web/relation_research_fit_you/follow?rid1={$itemData['rid1']}&rid2={$itemData['rid2']}&order_id=&history_id={$itemData['id']}&need_auth=2&zhunle_not_show_nav=1&zhunle_nav_text_color=1&refer_name=怎么追求Ta解锁记录列表" : 'empty',
            ];
        }

        $pageData['data'] = $resData;

        return show(config('code.success'), '请求成功', $pageData);
    }

    /**
     * 获取 鉴“渣”神器 解锁记录列表
     */
    public function getCheckPlayerList()
    {
        $pageData = (new Zl1RelationAnalysisReport())->getListPage($this->user['id'], 'id', ['rid1', 'create_time', 'id', 'recode_info', 'content', 'status'], 2, 'desc', 0);

        $resData = [];
        foreach ($pageData['data'] as $itemData)
        {
            $itemData = arrayKeyData2int($itemData, ['id', 'uid', 'status', 'gid', 'rid1', 'create_time']);

            if (isset($itemData['content']) && $itemData['content'])
            {
                $itemData['content'] = json_decode($itemData['content'], true);
            }

            if (isset($itemData['recode_info']) && $itemData['recode_info'])
            {
                $itemData['recode_info'] = json_decode($itemData['recode_info'], true);
            }
            // 计算状态 1待计算 2计算成功 3计算失败 4已退款 => 转换 状态 1=>计算中 2=>已解锁 3=>已退款
            $status = [
                1 => 1,
                2 => 2,
                3 => 1,
                4 => 3,
            ];

            if (in_array($itemData['status'], [1, 3], true))
            {   // 计算中
                $content = AuthenticatePlayboy::getInstance()->setUserRecord($itemData['recode_info']['recode1'])->free();
                $score = $content['probability'] ?? 50; // 默认50
            } else
            {   // 计算成功 ｜ 已退款
                $score = $itemData['content']['probability'] ?? 50; // 默认50
            }

            $resData[] = [
                'id' => $itemData['id'],
                'title' => '“渣”可能性',
                'status' => $status[$itemData['status']],
                'score' => $score,
                'avatar' => $itemData['recode_info']['recode1']['avatar'],
                'name' => $itemData['recode_info']['recode1']['id'] == $this->user['rid'] ? '自己' : $itemData['recode_info']['recode1']['name'],
                'target' => $status[$itemData['status']] === 2 ? 'link' : 'local',
                'link' => $status[$itemData['status']] === 2 ? Env::get('host.domain') . "/web/relation_research_fit_you/check_player?rid={$itemData['rid1']}&order_id=&history_id={$itemData['id']}&need_auth=2&zhunle_not_show_nav=1&zhunle_nav_text_color=1&refer_name=鉴渣神器解锁记录列表" : 'empty',
            ];
        }

        $pageData['data'] = $resData;

        return show(config('code.success'), '请求成功', $pageData);
    }

    /**
     * 关系研究所 报告删除
     */
    public function reportDel()
    {
        $id = input('id/d', 0);

        $relationAnalysisReportModel = new Zl1RelationAnalysisReport();

        $details = $relationAnalysisReportModel->getInfoById($id, ['uid', 'status', 'is_del', 'type', 'rid1', 'rid2']);

        if (!$details || $details['uid'] !== $this->user['id'] || $details['is_del'] === 1)
        {
            return show(config('code.error'), '服务异常，记录不存在或已删除～');
        }

        if (in_array($details['status'], [1, 3]))
        {
            return show(config('code.error'), '服务异常，计算中数据不能删除～');
        }

        $res = $relationAnalysisReportModel->DeleteData($id, $this->user['id'], $details['type'], $details['rid1'], $details['rid2']);

        if ($res)
        {
            return show(config('code.success'), '删除成功');
        } else
        {
            return show(config('code.error'), '服务异常，请稍后再试～');
        }
    }

    /**
     * 我的理想型 方案删除
     */
    public function MyIdeaCaseDelete()
    {
        $id = input('id/d', 0);

        $relationAnalysisIdeaModel = new Zl1RelationAnalysisIdea();

        $caseDetail = $relationAnalysisIdeaModel->getInfoById($id);

        if (!$caseDetail || $caseDetail['uid'] !== $this->user['id'])
        {
            return show(config('code.error'), '服务异常，方案不存在或已删除～');
        }

        $res = $relationAnalysisIdeaModel->DeleteUpdate($id, $this->user['id']);

        if ($res)
        {
            return show(config('code.success'), '删除成功');
        } else
        {
            return show(config('code.error'), '服务异常，请稍后再试～');
        }
    }

}
